<template>
  <div class="search-results-page">
    <Header />
    
    <main class="main">
      <div class="container">
        <div class="breadcrumb">
          <span class="breadcrumb-item" @click="$router.push('/')" style="cursor: pointer; color: #667eea;">
            <i class="fas fa-home"></i>
            首页
          </span>
          <i class="fas fa-chevron-right breadcrumb-separator"></i>
          <span class="breadcrumb-item active">搜索结果</span>
        </div>

        <div class="search-header">
          <div class="search-header-top">
            <button class="back-home-btn" @click="$router.push('/')">
              <i class="fas fa-arrow-left"></i>
              返回首页
            </button>
          </div>
          <h1 class="search-title">搜索结果</h1>
          <p class="search-subtitle">关键词："{{ searchQuery }}"</p>
          <p class="results-count">共找到 {{ searchResults.length }} 个结果</p>
        </div>

        <div class="search-filters">
          <div class="filter-group">
            <label>排序方式：</label>
            <select v-model="sortBy" class="filter-select">
              <option value="relevance">相关度</option>
              <option value="price-low">价格从低到高</option>
              <option value="price-high">价格从高到低</option>
              <option value="sales">销量</option>
            </select>
          </div>
        </div>

        <div class="search-results-grid">
          <div 
            class="product-card" 
            v-for="product in filteredResults" 
            :key="product.id"
            @click="handleProductClick(product)"
          >
            <div class="product-image">
              <img :src="product.image" :alt="product.title">
              <div class="product-discount" v-if="product.discount">-{{ product.discount }}%</div>
            </div>
            <div class="product-info">
              <div class="product-brand">{{ product.brand }}</div>
              <h3 class="product-title">{{ product.title }}</h3>
              <div class="product-price">
                <span class="current-price">¥{{ product.currentPrice }}</span>
                <span class="original-price" v-if="product.originalPrice">¥{{ product.originalPrice }}</span>
              </div>
              <div class="product-features">
                <span class="feature-tag" v-for="feature in product.features" :key="feature">{{ feature }}</span>
              </div>
              <div class="product-stats">
                <span class="sales-count">已售 {{ product.salesCount }}</span>
                <span class="rating">
                  <i class="fas fa-star" v-for="n in 5" :key="n"></i>
                  {{ product.rating }}
                </span>
              </div>
            </div>
          </div>
        </div>

        <div class="no-results" v-if="filteredResults.length === 0">
          <i class="fas fa-search"></i>
          <h3>未找到相关商品</h3>
          <p>请尝试其他关键词或调整搜索条件</p>
          <button class="btn btn-primary" @click="$router.push('/')">返回首页</button>
        </div>
      </div>
    </main>

    <Footer />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'

const route = useRoute()
const router = useRouter()
const searchQuery = ref('')
const sortBy = ref('relevance')

// 模拟搜索结果数据
const searchResults = ref([
  {
    id: 1,
    brand: 'Apple',
    title: 'iPhone 15 Pro 256GB 钛金色',
    currentPrice: '8,999',
    originalPrice: '10,199',
    discount: 12,
    image: 'https://images.unsplash.com/photo-1592750475338-74b7b21085ab?w=400&h=400&fit=crop&crop=center',
    features: ['正品保障', '全国联保', '顺丰包邮'],
    salesCount: '3.2k+',
    rating: '4.9'
  },
  {
    id: 2,
    brand: 'Samsung',
    title: 'Galaxy S24 Ultra 512GB 钛灰色',
    currentPrice: '9,199',
    originalPrice: '9,999',
    discount: 8,
    image: 'https://images.unsplash.com/photo-1574944985070-8f3ebc6b79d2?w=400&h=400&fit=crop&crop=center',
    features: ['AI拍照', 'S Pen', '200MP相机'],
    salesCount: '1.8k+',
    rating: '4.7'
  },
  {
    id: 3,
    brand: 'Huawei',
    title: 'P60 Pro 512GB 洛可可白',
    currentPrice: '6,299',
    originalPrice: '6,999',
    discount: 10,
    image: 'https://images.unsplash.com/photo-1574944985070-8f3ebc6b79d2?w=400&h=400&fit=crop&crop=center',
    features: ['徕卡影像', '昆仑玻璃', '66W快充'],
    salesCount: '2.5k+',
    rating: '4.8'
  }
])

// 根据排序方式过滤结果
const filteredResults = computed(() => {
  let results = [...searchResults.value]
  
  switch (sortBy.value) {
    case 'price-low':
      return results.sort((a, b) => parseInt(a.currentPrice.replace(',', '')) - parseInt(b.currentPrice.replace(',', '')))
    case 'price-high':
      return results.sort((a, b) => parseInt(b.currentPrice.replace(',', '')) - parseInt(a.currentPrice.replace(',', '')))
    case 'sales':
      return results.sort((a, b) => parseInt(b.salesCount.replace(/[^\d]/g, '')) - parseInt(a.salesCount.replace(/[^\d]/g, '')))
    default:
      return results
  }
})

// 商品点击处理
const handleProductClick = (product: any) => {
  router.push(`/product/${product.id}`)
}

onMounted(() => {
  searchQuery.value = route.query.q as string || ''
})
</script>

<style scoped>
.search-results-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding: 12px 0;
  font-size: 14px;
  color: #636e72;
}

.breadcrumb-item {
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.3s ease;
}

.breadcrumb-item:hover {
  color: #667eea;
}

.breadcrumb-item.active {
  color: #2d3436;
  font-weight: 500;
}

.breadcrumb-separator {
  font-size: 12px;
  color: #bdc3c7;
}

.search-header {
  text-align: center;
  margin-bottom: 30px;
}

.search-header-top {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 20px;
}

.back-home-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.back-home-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.back-home-btn i {
  font-size: 12px;
}

.search-title {
  font-size: 32px;
  font-weight: 700;
  color: #2d3436;
  margin-bottom: 10px;
}

.search-subtitle {
  font-size: 16px;
  color: #636e72;
  margin-bottom: 5px;
}

.results-count {
  font-size: 14px;
  color: #95a5a6;
}

.search-filters {
  margin-bottom: 30px;
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.filter-group label {
  font-weight: 500;
  color: #2d3436;
}

.filter-select {
  padding: 8px 12px;
  border: 1px solid #dee2e6;
  border-radius: 6px;
  background: white;
  color: #495057;
  font-size: 14px;
}

.search-results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 40px;
}

.product-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 30px rgba(0,0,0,0.15);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-discount {
  position: absolute;
  top: 12px;
  right: 12px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.product-info {
  padding: 20px;
}

.product-brand {
  font-size: 12px;
  color: #667eea;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.product-title {
  font-size: 16px;
  font-weight: 600;
  color: #2d3436;
  line-height: 1.4;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.current-price {
  font-size: 18px;
  font-weight: 700;
  color: #e74c3c;
}

.original-price {
  font-size: 14px;
  color: #bdc3c7;
  text-decoration: line-through;
}

.product-features {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.feature-tag {
  background: #f8f9fa;
  color: #636e72;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 500;
}

.product-stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #95a5a6;
}

.sales-count {
  font-weight: 500;
}

.rating {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 600;
}

.rating i {
  color: #f39c12;
  font-size: 10px;
}

.no-results {
  text-align: center;
  padding: 60px 20px;
  color: #636e72;
}

.no-results i {
  font-size: 48px;
  margin-bottom: 20px;
  color: #bdc3c7;
}

.no-results h3 {
  font-size: 24px;
  margin-bottom: 10px;
  color: #2d3436;
}

.no-results p {
  margin-bottom: 30px;
}

@media (max-width: 768px) {
  .breadcrumb {
    justify-content: center;
    font-size: 13px;
  }
  
  .search-header-top {
    justify-content: center;
  }
  
  .back-home-btn {
    padding: 8px 12px;
    font-size: 13px;
  }
  
  .search-title {
    font-size: 24px;
  }
  
  .search-results-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .product-card {
    margin-bottom: 16px;
  }
}
</style> 